package com.fenghongzhang.day008;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private Button spWrite;
    private Button spRead;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        spWrite = (Button) findViewById(R.id.sp_write);
        spRead = (Button) findViewById(R.id.sp_read);

        spWrite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1,得到sp对象
                /**
                 * 1, 文件名, 有就打开,没有就创建
                 */
                SharedPreferences sp = getSharedPreferences("login", MODE_PRIVATE);

                //2,编译器
                SharedPreferences.Editor edit = sp.edit();

                //3,存数据
                edit.putString("name","小明");
                edit.putBoolean("isMan",true);
                edit.putInt("age",16);
                edit.putFloat("age1",1.9f);
                edit.putLong("age2,",3);

                //4提交
                edit.commit();
            }
        });

        spRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences login = getSharedPreferences("login", MODE_PRIVATE);
                String name = login.getString("name","大明");
                String name1 = login.getString("name1","大明");//大明
                Log.i(TAG, "onClick: "+name);
                Log.i(TAG, "onClick: "+name1);

                boolean isLogin = login.getBoolean("isLogin", false);
                Log.i(TAG, "onClick: "+isLogin);
            }
        });

    }
}
